﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Projet_WPF.DataAccess
{
    class Patient
    {
        public List<Model.Patient> GetListPatient()
        {
            try
            {
                ServicePatient.ServicePatientClient service = new ServicePatient.ServicePatientClient();
                List<Model.Patient> list = new List<Model.Patient>();

                foreach (var patient in service.GetListPatient())
                {
                    List<Model.Observation> observations = new List<Model.Observation>();

                    foreach (var observation in patient.Observations)
	                {
		                Model.Observation o = Model.Observation.CreateObservation(observation.Date, observation.Comment, observation.Prescription, observation.Pictures, observation.Weight, observation.BloodPressure);
                        observations.Add(o);
	                }

                    Model.Patient p = Model.Patient.CreatePatient(patient.Name, patient.Firstname, patient.Birthday, patient.Id, observations);
                    list.Add(p);
                }

                return list;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public Model.Patient GetPatient(int id)
        {
            try
            {
                ServicePatient.ServicePatientClient service = new ServicePatient.ServicePatientClient();
                ServicePatient.Patient patient = service.GetPatient(id);
                List<Model.Observation> list = new List<Model.Observation>();

                foreach (var observation in patient.Observations)
	            {
		            Model.Observation o = Model.Observation.CreateObservation(observation.Date, observation.Comment, observation.Prescription, observation.Pictures, observation.Weight, observation.BloodPressure);
                    list.Add(o);
	            }

                Model.Patient p = Model.Patient.CreatePatient(patient.Name, patient.Firstname, patient.Birthday, patient.Id, list);
                
                return p;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool AddPatient(Model.Patient user)
        {
            try
            {
                ServicePatient.ServicePatientClient service = new ServicePatient.ServicePatientClient();
                ServicePatient.Patient p = new ServicePatient.Patient();

                List<ServicePatient.Observation> list = new List<ServicePatient.Observation>();

                foreach (var observation in user.Observations)
                {
                    ServicePatient.Observation o = new ServicePatient.Observation();

                    o.Date = observation.Date;
                    o.Comment = observation.Comment;
                    o.Prescription = observation.Prescription;
                    o.Pictures = observation.Pictures;
                    o.Weight = observation.Weight;
                    o.BloodPressure = observation.BloodPressure;

                    list.Add(o);
                }

                p.Name = user.Name;
                p.Firstname = user.Firstname;
                p.Birthday = user.Birthday;
                p.Id = user.Id;
                p.Observations = list.ToArray();

                return service.AddPatient(p);
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool DeletePatient(int id)
        {
            try
            {
                ServicePatient.ServicePatientClient service = new ServicePatient.ServicePatientClient();

                return service.DeletePatient(id);
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
